<!DOCTYPE html>
<meta charset="UTF-8">
<script type="text/html" id="toolbarDemo1">
	<div class="layui-btn-group ">
   		<button class="layui-btn layui-icon layui-icon-add-1" lay-event="add">新增</button>
  	  	<button class="layui-btn layui-icon layui-icon-edit" lay-event="update">修改</button>
  	  	<button class="layui-btn layui-icon layui-icon-delete" lay-event="delete">删除</button>
	</div>
</script>
<table id="menuTable" class="layui-table" lay-filter="menuTable"></table>
<script type="text/javascript">
	layui.use([ 'treetable', 'table', 'layer' ], function() {
		var table = layui.table;
		var layer = layui.layer;
		var treetable = layui.treetable;
		var crow = null;
		var tableData = null;
		//渲染表格
		var renderTable = function() {
			layer.load(2);
			treetable.render({
				//height:'full-160',
				id : 'menuTable',
				treeColIndex : 1, //树形图标显示在第几列
				treeSpid : '0', //最上级的父级id
				treeIdName : 'id', //id字段的名称
				treePidName : 'parentId', //pid字段的名称，父级菜单id
				treeDefaultClose : false, //是否默认折叠
				treeLinkage : false, //父级展开时是否自动展开所有子级
				elem : '#menuTable', //表格id
				url : 'menu/treeList',
				toolbar : '#toolbarDemo1',
				page : false,
				cols : [ 
					[ {
					type : 'checkbox'
				}, {
					field : 'name',
					title : '名称'
				},

				{
					field : 'role',
					title : '权限名称',
					templet : function(d) {
						return d.role ? d.role.name : '';
					}

				}, {
					field : 'url',
					title : '地址'
				},{
					field:'idx1',
					title:'排序'
				}

				] 
					],
				//数据渲染完的回调
				done : function() {
					//关闭加载
					layer.closeAll('loading');

				}
			})
		};
		renderTable();
		table.on('toolbar', function(obj) {
			switch (obj.event) {
			case 'add':
				add();
				break;
			case 'update':
				update();
				break;
			case 'delete':
				del();
				break;
			}
		});
		function add() {
			var checkStatus = table.checkStatus('menuTable');
			var data = checkStatus.data;
			var parentId = data.length == 1 ? data[0].id : '';
			 if (data.length >1) {
				layer.msg("请选择一个父节点进行新增!")
		} else{
			$.get('menu/edit', {parentId : parentId}, function(html) {
				layer.open({
					type : 1,
					title : '新增',
					area : [ '500px', '500px' ],
					content : html,
					btn : [ '提交', '退出' ],
					success : function(layero, index) {
						layui.use('form', function() {
							var form = layui.form;
							layero.addClass('layui-form');
							var submitBtn = layero.find('.layui-layer-btn0');
							submitBtn.attr('lay-filter', 'formVerify').attr(
									'lay-submit', '');
							layero.keydown(function(e) {
								if (e.keyCode == 13) {
									submitBtn.click();
								}
							});

							form.on('submit(formVerify)', function(data) {
								$.post('menu/save', data.field,
										function(result) {
											if (result.success) {
												layer.close(index);
												renderTable();
											}
											layer.msg(result);
										});
								return false;
							});
						});
					}

				});
			});
			}
		}
		function update() {
			var checkStatus = table.checkStatus('menuTable');
			var data = checkStatus.data;
			var parentId = data.length == 1 ? data[0].id : '';
			if (data.length != 1) {
				layer.alert('请先选择一条数据再进行修改操作！');
				return;
			}
			$.get('menu/edit', {
				id : parentId
			}, function(html) {
				layer.open({
					id : 'update',
					type : 1,
					title : '修改',
					area : [ '500px', '500px' ],
					content : html,
					btn : [ '提交', '退出' ],

					success : function(layero, index) {
						layui.use('form', function() {
							var form = layui.form;
							//让‘提交’按钮有提交功能
							layero.addClass('layui-form');
							var submitBtn = layero.find('.layui-layer-btn0');
							submitBtn.attr('lay-filter', 'formVerify').attr(
									'lay-submit', '');
							//在弹出层里绑定回车就提交表单
							layero.keydown(function(e) {
								if (e.keyCode == 13) {
									submitBtn.click();
								}
							});
							form.on('submit(formVerify)', function(data) {
								$.post('menu/save', data.field,
										function(result) {
											if (result.success) {
												layer.close(index);
												renderTable();
											}
											layer.msg(result.msg);
										});
								return false;
							});
						});
					}
				});

			}, 'html');
		}
		function del() {
			var checkStatus = table.checkStatus('menuTable');
			var data = checkStatus.data;
			var crId = data.length == 1 ? data[0].id : '';
			if (data.length != 1) {
				layer.msg("请选择一条进行删除！");
				return;
			}
			layer.confirm('真的删除选定行的数据吗?', function(index) {
				$.post('menu/delete', {
					id : crId
				}, function(result) {
					if (result.success) {
						layer.close(index);
						crow = null;
						renderTable();
					}
					layer.msg(result.msg);
				});
			});
		}
	});
</script>
